package com.example.firstapp;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.firstapp.entity.Phone;
import service.PhoneService;
import util.Database;
import util.MyDatabaseHelper;

public class Test04Activity extends AppCompatActivity {
    private ListView lv_phoneNumber;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test04);

        //链接数据库
        MyDatabaseHelper helper = new MyDatabaseHelper(this, "FistApp.db",
                null, 1);

        //获取数据库对象
        db = helper.getWritableDatabase();

        //绑定组件
        EditText et_name = findViewById(R.id.et_name);
        EditText et_phone = findViewById(R.id.et_photo);
        Button bt_phone_add = findViewById(R.id.bt_photo_add);
        Button bt_phone_delete = findViewById(R.id.bt_photo_delete);
        Button bt_phone_update = findViewById(R.id.bt_photo_update);
        Button bt_phone_select = findViewById(R.id.bt_photo_select);
        Button bt_updateList = findViewById(R.id.bt_updateList);
        lv_phoneNumber = findViewById(R.id.lv_phoneNumber);

        //刷新通讯录列表
        updateList(db, null, null);

        /*
         * 监听组件
         * 用匿名监听组件
         */
        //添加按键绑定
        bt_phone_add.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString(), et_phone.getText().toString());
            PhoneService.phone_add(db, phone);
            if (phone.isState())
                updateList(db, null, null);
            message(phone.getMessage());
        });

        //删除按键绑定
        bt_phone_delete.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString());
            PhoneService.phone_delete(db, phone);
            if (phone.isState()) {
                et_name.setText("");
                et_phone.setText("");
            }
            if (phone.isState())
                updateList(db, null, null);
            message(phone.getMessage());
        });

        //修改按键绑定
        bt_phone_update.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString(), et_phone.getText().toString());
            PhoneService.phone_update(db, phone);
            if (phone.isState())
                updateList(db, null, null);
            message(phone.getMessage());
        });

        //查询按键绑定
        bt_phone_select.setOnClickListener(view -> {
            Phone phone = new Phone(et_name.getText().toString());
            PhoneService.phone_select(db, phone);
            et_phone.setText(phone.getPhoneNumber());
            if (phone.isState())
                updateList(db, "name=?", new String[]{phone.getName()});
            message(phone.getMessage());
        });

        //刷新按键绑定
        bt_updateList.setOnClickListener(view -> {
            updateList(db, null, null);
        });
    }

    public void updateList(SQLiteDatabase db, String selection, String[] selectionArgs) {
        SimpleAdapter adapter = new SimpleAdapter(Test04Activity.this,
                Database.select_allPhone(db, "phoneNumber", selection, selectionArgs,null),
                R.layout.list_item,
                new String[]{"ico", "name", "phoneNumber"},
                new int[]{R.id.lv_img, R.id.tv_lv_name, R.id.tv_lv_text});
        lv_phoneNumber.setAdapter(adapter);
    }

    private void message(String message) {
        Toast.makeText(Test04Activity.this, message, Toast.LENGTH_LONG).show();
    }
}